<?php
declare (strict_types = 1);

namespace app\index\controller;

use app\common\model\MessagesModel;
use app\common\model\SinglepageModel;
use think\facade\View;

class Contact extends Common
{
    public function index(){    //获取评论总数
        $info = SinglepageModel::where('varname','contact')->where('lang','=',$this->lang)->find();
        $commlist = $this->getCommlist();   //获取评论列表
        $page = $this->getCommlist();
	    $head_id = $info['varname'];
	    View::assign('head_id',$head_id);
        View::assign('info',$info);
        return view('index',['commlist'=>$commlist,'page'=>$page]);
    } 
    
    public function getCommlist($pcode=''){    
        $res = MessagesModel::where('pcode',$pcode)->where('pid',0)->where('status','1')->order('create_time desc')->paginate(20,false,['query' => request()->param(),'type' => 'page\Page','var_page'  => 'page']);
		foreach($res as $k => $v){
		    if(!$v['phone_s']){
		      $messCount = MessagesModel::where("nickname",$v['nickname'])->where('status','1')->count();  
		    }else{
		      $messCount = MessagesModel::where("phone_s",$v['phone_s'])->where('status','1')->count();  
		    }
		    if($messCount < 10){
		        $res[$k]['diamond'] = '评论先锋 LV.1';    
		    }else if($messCount < 20){
		        $res[$k]['diamond'] = '评论达人 LV.2';
		    }else if($messCount < 30){
		        $res[$k]['diamond'] = '评论精英 LV.3';
		    }else if($messCount < 50){
		        $res[$k]['diamond'] = '评论大师 LV.4';
		    }else if($messCount < 70){
		        $res[$k]['diamond'] = '评论王者 LV.5';
		    }
	        $res2 = MessagesModel::where("pcode",$v['id'])->where('status','1')->select();
		    $res[$k]['children'] = $res2;
		}
		return $res;
    }
    
    // public function show_comments($pcode=0,$margin=0){
    //     $pid = input('id');
    //     $comments = MessagesModel::where('pcode',$pcode)->where('pid','0')->where('status','1')->paginate(10);
    //     $html ='';
    //     //非一级评论 循环一层 左增60px.
    //     if($pcode>0){
    //         $margin='child';
    //     }
    //     //pid有数据对结果进行循环数据嵌HTML
    //     if(count($comments)>0){
    //         foreach($comments as $comment){
    //             $html.='<li class="comment '.$margin.' rounded">';
    //             $html.='<div class="thumb">';
    //             $html.='<img src="'.$comment['avatar'].'" class="messanava" alt="'.$comment['nickname'].'" />';
    //             $html.='</div>';
    //             $html.='<div class="details">';
    //             $html.='<h4 class="name"><a href="../">'.$comment['nickname'].'</a></h4>';
    //             $html.='<span class="date">'.$comment['create_time'].'</span>';
    //             $html.='<p>'.$comment['content'].'</p>';
    //             $html.='</div>';
    //             $html.='</li>';
    //             //每次循环后都再次调用函数自身
    //             $html.=$this->show_comments($comment['id'],$margin);
    //         }
    //     }
    //     return $html;
        
    // }
}
